﻿using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Utils;

namespace DataBaseConnect
{
    public class MenuRepository : FonourRepositoryBase<Menu>, IMenuRepository
    {
        public MenuRepository(MyDataBase dbContext) : base(dbContext)
        {

        }

        /// <summary>
        /// 根据权限获取菜单信息
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public List<Menu> GetMenuList(int userId)
        {
            //根据userid得到角色id
            var roleId = _dbContext.Set<UserRole>().Where(u => u.UserId == userId).Select(f => f.RoleId).FirstOrDefault();
            //根据角色id得到菜单id
            var menuId = _dbContext.Set<MenuRole>().Where(d => d.RoleId == roleId).ToList();
            //关联查询
            var menu = (from q in _dbContext.Set<Menu>().ToList()
                        join m in menuId.ToList()
                        on q.MenuId equals m.MenuId
                        select q).ToList();

            //根据菜单id得到菜单
            return menu;
        }

        /// <summary>
        /// 根据条件删除对应的菜单菜单权限
        /// </summary>
        /// <param name="where">删除条件</param>
        /// <returns></returns>
        public ReceiveStatus DelMenuRoleMsg(Expression<Func<MenuRole, bool>> where)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _dbContext.Set<MenuRole>().Where(where).ToList().ForEach(it => _dbContext.Set<MenuRole>().Remove(it));
                _dbContext.SaveChanges();
                receiveStatus.msg = "删除成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据角色id获得菜单权限
        /// </summary>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public List<MenuRole> GetRoleIdList(int RoleId)
        {
            return _dbContext.Set<MenuRole>().Where(f => f.RoleId == RoleId).ToList();
        }

        /// <summary>
        /// 递归删除菜单
        /// </summary>
        /// <param name="MenuId">菜单id</param>
        /// <returns></returns>
        public ReceiveStatus DelForMneuMsg(int MenuId)
        {
            List<Menu> list = new List<Menu>();
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                var ListId = _dbContext.Set<Menu>().Where(f => f.MenuParentId == MenuId).ToList();
                //删除节点
                _dbContext.Set<Menu>().Where(f => f.MenuId == MenuId).ToList().ForEach(it => _dbContext.Set<Menu>().Remove(it));
                _dbContext.SaveChanges();
                if (ListId.Count != 0)
                {
                    //删除下级菜单
                    foreach (var item in ListId)
                    {
                        _dbContext.Set<Menu>().Where(f => f.MenuId == item.MenuId).ToList().ForEach(it => _dbContext.Set<Menu>().Remove(it));
                        _dbContext.SaveChanges();
                        list.AddRange(GetForMenuListMsg(item.MenuId)); // 当前菜单的下级菜单集合
                        receiveStatus.msg = "删除成功";
                    }
                }
                ListId = list; // 最新集合
                foreach (var item in ListId)
                {
                    DelForMneuMsg(item.MenuId); // 递归
                }
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg ="删除失败"+ ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 获取下级菜单集合
        /// </summary>
        /// <param name="MenuId">菜单id</param>
        /// <returns></returns>
        public List<Menu> GetForMenuListMsg(int MenuId)
        {
            return _dbContext.Set<Menu>().Where(f => f.MenuParentId == MenuId).ToList();
        }
    }
}
